package com.woniuxy.provider.listener;

import com.woniuxy.provider.service.StuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
@Slf4j
public class RedisDeleteListener {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    // 监听 Redis 删除队列
    @RabbitListener(queues = "student.delete.redis.queue")
    public void deleteFromRedis(int id) {

        // 查找匹配的键并删除
        String pattern = "stu:*"; // 修改为 Redis 中实际使用的模式
        Set<String> keys = redisTemplate.keys(pattern);
        if (keys != null && !keys.isEmpty()) {
            for (String key : keys) {
                redisTemplate.delete(key);
                log.info("从 Redis 中删除学生数据，ID: " + id + "，删除的键: " + key);
            }
        } else {
            log.warn("没有找到匹配的 Redis 键，ID: " + id);
        }

    }
}
